#include "OptionsState.h"

#include "..\\Definitions.h"
#include "..\Game.h"
#include "MenuState.h"

#include "..\Bitmap Font\BitmapFont.h"

#include "..\..\SGD Wrappers\SGD_AudioManager.h"
#include "..\..\SGD Wrappers\SGD_GraphicsManager.h"
#include "..\..\SGD Wrappers\SGD_InputManager.h"
#include "..\..\SGD Wrappers\SGD_String.h"

#include <fstream>

/**************************************************************/
// GetInstance
//	- store the ONLY instance in global memory
//	- return the only instance by pointer
/*static*/ OptionsState* OptionsState::GetInstance(void)
{
	static OptionsState s_Instance;

	return &s_Instance;
}

void OptionsState::Enter()
{
	//GAME->PlayBGM(false);

	hBackground = GRAPHICS->LoadTexture(L"Resource/Images/MenuBackground.png");

	// Load Assets
	//hBackgroundMusic = Audio->LoadAudio(L"Resource/Beautiful_Chinese_Music_-_Cherry_Blossoms.xwm");
	hSfx = AUDIO->LoadAudio(L"Resource/Audio/(Throw)Whoosh.wav");

	// Load Audio Volume
	nMusicVol = GAME->GetMusicVolume();
	nSfxVol = GAME->GetSfxVolume();
	bFullscreen = GAME->GetFullScreen();

	nCursor = 0;
}

void OptionsState::Exit()
{
	std::wstring filePath = GAME->GetFilePath();
	filePath += L"\\options.txt";
	std::fstream file;
	file.open(filePath.c_str(), std::ios_base::out);
	if (file.is_open())
	{
		file << nMusicVol << '\n' << nSfxVol << '\n' << bFullscreen;
		file.close();
	}
	GAME->SetMusicVolume(nMusicVol);
	GAME->SetSfxVolume(nSfxVol);
	GAME->SetFullScreen(bFullscreen);

	// Unload assets
	GRAPHICS->UnloadTexture(hBackground);
	//SGD::AudioManager::GetInstance()->UnloadAudio(hBackgroundMusic);
	AUDIO->UnloadAudio(hSfx);

}

bool OptionsState::Update(float dt)
{
	SGD::InputManager* Input = SGD::InputManager::GetInstance();
	
	if (KEYUPPRESSED)
	{
		GAME->SetInput(false);
		--nCursor;

		bPlayAudio = true;

		// Wrap around
		if (nCursor < 0)
		{
			nCursor = 3;
			bPlayAudio = false;
		}
	}
	else if (KEYDOWNPRESSED)
	{
		GAME->SetInput(false);
		++nCursor;
		bPlayAudio = true;

		// Wrap around
		if (nCursor > 3)
			nCursor = 0;
		if (nCursor == 3)
			bPlayAudio = false;
	}
	if (nCursor != 0)
	{
		//Audio->StopAudio(hBackgroundMusic);
	}
	if (nCursor == 0)
	{
		if (bPlayAudio)
		{
			//Audio->PlayAudio(hBackgroundMusic);
			bPlayAudio = false;
		}
		if (KEYLEFTPRESSED)
		{
			nMusicVol -= 10;
			if (nMusicVol < 0)
				nMusicVol = 0;
			AUDIO->SetMasterVolume(SGD::AudioGroup::Music, nMusicVol);
		}
		else if (KEYRIGHTPRESSED)
		{
			nMusicVol += 10;
			if (nMusicVol > 100)
				nMusicVol = 100;
			AUDIO->SetMasterVolume(SGD::AudioGroup::Music, nMusicVol);
		}
	}
	else if (nCursor == 1)
	{
		if (bPlayAudio)
		{
			AUDIO->PlayAudio(hSfx);
			bPlayAudio = false;
		}
		if (KEYLEFTPRESSED)
		{
			GAME->SetInput(false);
			nSfxVol -= 10;

			if (nSfxVol < 0)
				nSfxVol = 0;
			AUDIO->SetMasterVolume(SGD::AudioGroup::SoundEffects, nSfxVol);
			AUDIO->PlayAudio(hSfx);
		}
		else if (KEYRIGHTPRESSED)
		{
			GAME->SetInput(false);
			nSfxVol += 10;
			if (nSfxVol > 100)
				nSfxVol = 100;
			AUDIO->SetMasterVolume(SGD::AudioGroup::SoundEffects, nSfxVol);
			AUDIO->PlayAudio(hSfx);
		}
	}
	else if (nCursor == 2)
	{
		if (!bFullscreen && Input->IsKeyPressed(SGD::Key::Enter) || Input->IsButtonPressed(0, 0))
		{
			SGD::Size size{ GAME->GetScreenWidth(), GAME->GetScreenHeight() };
			GRAPHICS->Resize(size, false);
			bFullscreen = true;
		}
		else if (bFullscreen && Input->IsKeyPressed(SGD::Key::Enter) || Input->IsButtonPressed(0, 0))
		{
			SGD::Size size{ GAME->GetScreenWidth(), GAME->GetScreenHeight() };
			GRAPHICS->Resize(size, true);
			bFullscreen = false;
		}
	}
	
	if (Input->IsKeyPressed(SGD::Key::Escape) || Input->IsButtonPressed(0, 1))
		nCursor = 3;
	if (nCursor == 3)
	{
		if (Input->IsKeyPressed(SGD::Key::Enter) || Input->IsButtonPressed(0, 0))
			GAME->ChangeState(MenuState::GetInstance());
	}

	return true;
}

void OptionsState::Render()
{
	// Draw Background
	GRAPHICS->DrawTexture(hBackground, SGD::Point{ 0, 0 });

	// Display the game title
	GRAPHICS->DrawTexture(GAME->hOptionsImage, SGD::Point{ 125, 110 });

	// Display menu options
	
	// Display menu options
	SGD::Color notSelected = { 255, 225, 100 };
	SGD::Color Background, SFX, Fullscreen, Exit;
	Background = SFX = Fullscreen = Exit = notSelected;

	switch (nCursor)
	{
	case 0:
		Background = SGD::Color{ 255, 0, 0 };
		break;
	case 1:
		SFX = SGD::Color{ 255, 0, 0 };
		break;
	case 2:
		Fullscreen = SGD::Color{ 255, 0, 0 };
		break;
	case 3:
		Exit = SGD::Color{ 255, 0, 0 };
		break;
	}

	SGD::OStringStream ossBackGround;
	ossBackGround << nMusicVol;
	GAME->GetBitmapFont()->Render("Music", SGD::Point{ 300, 285 }, 0.5f, Background);
	GAME->GetBitmapFont()->Render(ossBackGround.str().c_str(), SGD::Point{ 450, 285 }, 0.5f, Background);

	SGD::OStringStream ossSfx;
	ossSfx << nSfxVol;
	GAME->GetBitmapFont()->Render("SFX", SGD::Point{ 300, 320 }, 0.5f, SFX);
	GAME->GetBitmapFont()->Render(ossSfx.str().c_str(), SGD::Point{ 450, 320 }, 0.5f, SFX);

	GAME->GetBitmapFont()->Render("Fullscreen", SGD::Point{ 300, 355 }, 0.5f, Fullscreen);
	if (bFullscreen)
		GAME->GetBitmapFont()->Render("Yes", SGD::Point{ 450, 355 }, 0.5f, Fullscreen);
	else
		GAME->GetBitmapFont()->Render("No", SGD::Point{ 450, 355 }, 0.5f, Fullscreen);

	GAME->GetBitmapFont()->Render("Exit", SGD::Point{ 300, 390 }, 0.5f, Exit);


	GAME->GetBitmapFont()->Render("Up/Down To Navigate, Enter/Button A To Select", SGD::Point{ 75, 525 }, 0.5f, SGD::Color{ 0, 250, 250 });
	GAME->GetBitmapFont()->Render("Escape/Button B To Go To Exit", SGD::Point{ 75, 550 }, 0.5f, SGD::Color{ 0, 250, 250 });
}